﻿using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
using System;
using System.Drawing;

namespace FinancialReportPowerPointAddIn.Helper
{
    public static class Common
    {
        public static void GetPLNameAndCode(string restaurantName, out string flag, out string code)
        {
            switch (restaurantName)
            {
                case "潭江店":
                    flag = "潭江";
                    code = "1420040";
                    break;
                case "开大店":
                    flag = "开平大道";
                    code = "1420322";
                    break;
                case "白石店":
                    flag = "白石";
                    code = "1420332";
                    break;
                case "西粤店":
                    flag = "西粤";
                    code = "1420396";
                    break;
                case "东湖店":
                    flag = "东湖";
                    code = "1420079";
                    break;
                case "恩平店":
                    flag = "恩平";
                    code = "1420363";
                    break;
                case "油十店":
                    flag = "油十";
                    code = "1420411";
                    break;
                case "汇总":
                    flag = "总表";
                    code = "总表";
                    break;
                default:
                    flag = string.Empty;
                    code = string.Empty;
                    break;
            }
        }

        public static void OrganizeFormat(Table table,float titlesize =14,float textsize = 12)
        {
            for (int colIndex = 1; colIndex <= table.Columns.Count; colIndex++)
            {
                for (int rowIndex = 1; rowIndex <= table.Rows.Count; rowIndex++)
                {
                    Cell cell = table.Cell(rowIndex, colIndex);
                    if (rowIndex == 1)
                    {
                        cell.Shape.TextFrame.TextRange.Font.Size = titlesize;
                    }
                    else
                    {
                        cell.Shape.TextFrame.TextRange.Font.Size = textsize;
                    }
                    cell.Shape.TextFrame.TextRange.Font.Name = "新宋体";
                    //cell.Shape.TextFrame.TextRange.Font.Color.RGB = System.Drawing.Color.FromArgb(0, 0, 0).ToArgb();
                    cell.Shape.TextFrame.TextRange.ParagraphFormat.Alignment = PpParagraphAlignment.ppAlignCenter;
                    cell.Shape.TextFrame.VerticalAnchor = Microsoft.Office.Core.MsoVerticalAnchor.msoAnchorMiddle;      
                    cell.Shape.TextFrame.MarginTop = 0;
                    cell.Shape.TextFrame.MarginLeft = 0;
                    cell.Shape.TextFrame.MarginRight = 0;
                    cell.Shape.TextFrame.MarginBottom = 0;
                }
            }            
        }

        public static void OrganizeFormat2(Table table)
        {
            for (int colIndex = 1; colIndex <= table.Columns.Count; colIndex++)
            {
                for (int rowIndex = 1; rowIndex <= table.Rows.Count; rowIndex++)
                {
                    Cell cell = table.Cell(rowIndex, colIndex);
                    cell.Shape.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
                    if (rowIndex == 1)
                    {
                        cell.Borders[PpBorderType.ppBorderBottom].Weight = 1;
                    }                    
                    //清除填充状态
                    cell.Borders[PpBorderType.ppBorderTop].Visible = MsoTriState.msoTrue;
                    //
                    cell.Borders[PpBorderType.ppBorderTop].ForeColor.RGB = ColorTranslator.ToOle(Color.FromArgb(218, 165, 32));
                    cell.Borders[PpBorderType.ppBorderBottom].Visible = MsoTriState.msoTrue;
                    cell.Borders[PpBorderType.ppBorderBottom].ForeColor.RGB = ColorTranslator.ToOle(Color.FromArgb(218, 165, 32));
                    if (colIndex ==1)
                    {
                        cell.Borders[PpBorderType.ppBorderLeft].Visible = MsoTriState.msoTrue;
                        cell.Borders[PpBorderType.ppBorderLeft].ForeColor.RGB = ColorTranslator.ToOle(Color.FromArgb(218,165,32));
                    }
                    if (colIndex == table.Columns.Count)
                    {
                        cell.Borders[PpBorderType.ppBorderRight].Visible = MsoTriState.msoTrue;
                        cell.Borders[PpBorderType.ppBorderRight].ForeColor.RGB = ColorTranslator.ToOle(Color.FromArgb(218, 165, 32));
                    }
                    if (colIndex == 8)
                    {
                        cell.Borders[PpBorderType.ppBorderLeft].Visible = MsoTriState.msoTrue;
                        cell.Borders[PpBorderType.ppBorderLeft].ForeColor.RGB = ColorTranslator.ToOle(Color.FromArgb(255, 0, 0));
                        cell.Borders[PpBorderType.ppBorderLeft].Weight = 2;
                    }
                    if (colIndex == 11)
                    {
                        cell.Borders[PpBorderType.ppBorderRight].Visible = MsoTriState.msoTrue;
                        cell.Borders[PpBorderType.ppBorderRight].ForeColor.RGB = ColorTranslator.ToOle(Color.FromArgb(255, 0, 0));
                        cell.Borders[PpBorderType.ppBorderRight].Weight = 2;
                    }
                    if (rowIndex ==1 && colIndex >= 8)
                    {
                        cell.Borders[PpBorderType.ppBorderTop].Visible = MsoTriState.msoTrue;
                        cell.Borders[PpBorderType.ppBorderTop].ForeColor.RGB = ColorTranslator.ToOle(Color.FromArgb(255, 0, 0));
                        cell.Borders[PpBorderType.ppBorderTop].Weight = 2;
                    }
                    if (rowIndex == table.Rows.Count && colIndex >= 8)
                    {
                        cell.Borders[PpBorderType.ppBorderBottom].Visible = MsoTriState.msoTrue;
                        cell.Borders[PpBorderType.ppBorderBottom].ForeColor.RGB = ColorTranslator.ToOle(Color.FromArgb(255, 0, 0));
                        cell.Borders[PpBorderType.ppBorderBottom].Weight = 2;
                    }
                }
            }
            // 填充表格的第2\3\4\5列
            foreach (Row row in table.Rows)
            {
                row.Cells[2].Shape.Fill.ForeColor.RGB = ColorTranslator.ToOle(Color.FromArgb(224,238,224));
                row.Cells[3].Shape.Fill.ForeColor.RGB = ColorTranslator.ToOle(Color.FromArgb(224,238,224));
                row.Cells[4].Shape.Fill.ForeColor.RGB = ColorTranslator.ToOle(Color.FromArgb(255,228,225));
                row.Cells[5].Shape.Fill.ForeColor.RGB = ColorTranslator.ToOle(Color.FromArgb(255,228,225));
            }
        }

        public static void AutoFit(Table table)
        {
            //计算每列的最大宽度
            float[] maxWidths = new float[table.Columns.Count];
            for (int colIndex = 1; colIndex <= table.Columns.Count; colIndex++)
            {
                float maxColumnWidth = 0;
                for (int rowIndex = 2; rowIndex <= table.Rows.Count; rowIndex++)
                {
                    Cell cell = table.Cell(rowIndex, colIndex);
                    float cellWidth = GetCellContentWidth(cell);
                    if (cellWidth > maxColumnWidth)
                    {
                        maxColumnWidth = cellWidth;
                    }
                }
                maxWidths[colIndex - 1] = maxColumnWidth;
            }

            // 将最大宽度应用到每一列
            for (int colIndex = 1; colIndex <= table.Columns.Count; colIndex++)
            {
                table.Columns[colIndex].Width = maxWidths[colIndex - 1];
            }
        }

        public static float GetCellContentWidth(Cell cell)
        {
            float maxWidth = 0;
            float currentWidth = 0;
            foreach (TextRange textRange in cell.Shape.TextFrame.TextRange.Paragraphs())
            {
                float childWidth = 0;
                foreach (TextRange item in textRange.Characters())
                {
                    childWidth += item.BoundWidth;
                }
                currentWidth = childWidth;
                if (currentWidth > maxWidth)
                {
                    maxWidth = currentWidth;
                }
            }
            cell.Shape.TextFrame.MarginLeft = 0;
            cell.Shape.TextFrame.MarginRight = 0;
            //maxWidth = maxWidth + cell.Shape.TextFrame.MarginLeft + cell.Shape.TextFrame.MarginRight + 1;
            return maxWidth;
        }

        //sales就在正常值,上升是绿色,下降是红色
        public static void WriteVar(Table table, int row, int col, decimal temp, string type)
        {
            if (temp >= 0)
            {
                table.Cell(row, col).Shape.TextFrame.TextRange.Text = "▲" + temp.ToString(type);
                table.Cell(row, col).Shape.TextFrame.TextRange.Font.Color.RGB = Color.FromArgb(0, 205, 0).ToArgb();     
            }
            else
            {
                table.Cell(row, col).Shape.TextFrame.TextRange.Text = "▼" + Math.Abs(temp).ToString(type);
                table.Cell(row, col).Shape.TextFrame.TextRange.Font.Color.RGB = Color.FromArgb(0, 0, 255).ToArgb();               
            }
        }        
    }
}
